/*
https://leetcode-cn.com/problems/longest-consecutive-sequence/solution/zui-chang-lian-xu-xu-lie-by-leetcode-solution/
 */
import java.util.HashSet;
import java.util.Set;

public class Solution128 {
    public int longestConsecutive(int[] nums) {
        if (nums.length==0){
            return 0;
        }
        Set<Integer> set=new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int ans=1;
        for (int num : nums) {
            if (!set.contains(num-1)){
                int t=num,c=1;
                while (set.contains(t+1)){
                    t++;
                    c++;
                }
                ans=Math.max(ans,c);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution128().longestConsecutive(new int[]{0,3,7,2,5,8,4,6,0,1}));
    }
}
